package com.estronger.shopmanage.ui.aftermarket.views;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.estronger.shopmanage.R;
import com.estronger.shopmanage.ui.aftermarket.adapter.MediaAdapter;
import com.estronger.shopmanage.ui.aftermarket.bean.AftermarketItem;
import com.estronger.shopmanage.ui.aftermarket.bean.MediaGridBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MediaView extends RelativeLayout {

    Context mContext;
    @BindView(R.id.media_recycle)
    RecyclerView recyclerView;
    MediaAdapter mediaAdapter;

    public int getItemCount(){
        return mediaAdapter.getItemCount();
    }

    public MediaView(Context context) {
        super(context);
    }

    public MediaView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        initView();
    }

    void initView(){
        View view = View.inflate(mContext, R.layout.appointment_media_layout,this);
        ButterKnife.bind(this,view);

        mediaAdapter = new MediaAdapter(mContext);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext,5);
        gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (mediaAdapter.getItemViewType(position) == 0){
                    return 5;
                }else if (mediaAdapter.getItemViewType(position) == 1){
                    return 1;
                }
                return -1;
            }
        });
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(mediaAdapter);
    }

    public void setWoattachImages(List<AftermarketItem.ImageDetaimItem> imageDatasource){

        if (mediaAdapter != null){

            List<MediaGridBean> problemArray = new ArrayList<>();
            List<MediaGridBean> problemVideo = new ArrayList<>();
            for (AftermarketItem.ImageDetaimItem item : imageDatasource) {
                MediaGridBean bean = new MediaGridBean();
                bean.setViewType(MediaGridBean.ViewType.ITEM);
                bean.setfType(item.getFtype());
                if (item.getFtype().equals("0")){
                    bean.setUrl(item.getFpath());
                    problemArray.add(bean);
                }else {
                    bean.setUrl(item.getFvideoimgpath());
                    problemVideo.add(bean);
                }
            }
            if (problemArray.size() > 0){
                MediaGridBean tbean = new MediaGridBean();
                tbean.setViewType(MediaGridBean.ViewType.TIEL);
                tbean.setName("问题图片");
                mediaAdapter.getGridBeanList().add(tbean);
                mediaAdapter.getGridBeanList().addAll(problemArray);
            }
            if (problemVideo.size() > 0){
                MediaGridBean vtbean = new MediaGridBean();
                vtbean.setViewType(MediaGridBean.ViewType.TIEL);
                vtbean.setName("问题视频");
                mediaAdapter.getGridBeanList().add(vtbean);
                mediaAdapter.getGridBeanList().addAll(problemVideo);
            }
            mediaAdapter.notifyDataSetChanged();
        }
    }

    public void setImageDatasource(List<AftermarketItem.ImageDetaimItem> imageDatasource){

        List<MediaGridBean> beforArray = new ArrayList<>();
        List<MediaGridBean> beforVideo= new ArrayList<>();
        List<MediaGridBean> afterArray = new ArrayList<>();
        List<MediaGridBean> afterVideo = new ArrayList<>();
        for (AftermarketItem.ImageDetaimItem item : imageDatasource){
            MediaGridBean bean = new MediaGridBean();
            bean.setViewType(MediaGridBean.ViewType.ITEM);
            bean.setfType(item.getFtype());
            if (item.getFphasetype().equals("0")){
                if (item.getFtype().equals("0")){
                    bean.setUrl(item.getFpath());
                    beforArray.add(bean);
                }else {
                    bean.setUrl(item.getFvideoimgpath());
                    beforVideo.add(bean);
                }
            }else {
                if (item.getFtype().equals("0")){
                    bean.setUrl(item.getFpath());
                    afterArray.add(bean);
                }else {
                    afterVideo.add(bean);
                    bean.setUrl(item.getFvideoimgpath());
                }
            }
        }
        if (mediaAdapter != null){
            if (beforArray.size() > 0){
                MediaGridBean bean = new MediaGridBean();
                bean.setViewType(MediaGridBean.ViewType.TIEL);
                bean.setName("完工前图片");
                mediaAdapter.getGridBeanList().add(bean);
                mediaAdapter.getGridBeanList().addAll(beforArray);
            }
            if (beforVideo.size() > 0){
                MediaGridBean bean = new MediaGridBean();
                bean.setViewType(MediaGridBean.ViewType.TIEL);
                bean.setName("完工前视频");
                mediaAdapter.getGridBeanList().add(bean);
                mediaAdapter.getGridBeanList().addAll(beforVideo);
            }

            if (afterArray.size() > 0){
                MediaGridBean bean = new MediaGridBean();
                bean.setViewType(MediaGridBean.ViewType.TIEL);
                bean.setName("完工后图片");
                mediaAdapter.getGridBeanList().add(bean);
                mediaAdapter.getGridBeanList().addAll(afterArray);
            }
            if (afterVideo.size() > 0){
                MediaGridBean bean = new MediaGridBean();
                bean.setViewType(MediaGridBean.ViewType.TIEL);
                bean.setName("完工后视频");
                mediaAdapter.getGridBeanList().add(bean);
                mediaAdapter.getGridBeanList().addAll(afterVideo);
            }
            mediaAdapter.notifyDataSetChanged();
        }
    }

}
